package cn.iocoder.yudao.module.trade.order.service.calculator;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.api.promotion.activity.enums.PromotionTypeEnum;
import cn.iocoder.yudao.module.api.promotion.groupon.dto.GrouponPackageRespDTO;
import cn.iocoder.yudao.module.api.trade.order.enums.TradeOrderTypeEnum;
import cn.iocoder.yudao.module.trade.order.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.order.bo.TradePriceCalculateRespBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import static cn.iocoder.yudao.module.trade.order.service.calculator.TradePriceCalculatorHelper.formatPrice;

/**
 * 团购套餐的 {@link TradePriceCalculator} 实现类
 *
 * @author 山野羡民
 */
@Slf4j
@Component
@Order(TradePriceCalculator.ORDER_GROUPON_PACKAGE)
public class TradeGrouponPackagePriceCalculator implements TradePriceCalculator {

    @Override
    public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) {
        // 团购套餐订单，才计算该优惠
        if (!TradeOrderTypeEnum.GROUPON.getType().equals(result.getType()) || result.getGrouponPackage() == null) {
            log.debug("[calculate][只有团购套餐订单({})，才处理团购套餐价格]", result.getType());
            return;
        }
        calculateNormalPrice(param, result);
        // 是否使用会员价
        if (param.isUseVipPrice()) {
            if (Boolean.TRUE.equals(result.getValidVip())) {
                // 是否有团购会员价
                if (result.getGrouponPackage().getVipPrice() > 0) {
                    log.debug("[calculate][团购套餐订单满足会员价条件：使用会员价+会员有效+团购套餐有会员价] userId={}", param.getUserId());
                    calculateVipPrice(param, result);
                } else {
                    log.debug("[calculate][grouponPackageId({}) 团购套餐订单不满足会员价条件，团购套餐没有会员价]", param.getGrouponPackageId());
                }
            } else {
                log.debug("[calculate][团购套餐订单不满足会员价条件，会员无效] userId={}", param.getUserId());
            }
        } else {
            log.debug("[calculate][团购套餐订单不满足会员价条件，未指定使用会员价] userId={}", param.getUserId());
        }
        TradePriceCalculatorHelper.recountAllPrice(result);
        log.debug("[calculate][团购套餐价格计算完成] {}", result.getPrice());
    }

    private void calculateNormalPrice(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) {
        GrouponPackageRespDTO grouponPackage = result.getGrouponPackage();
        result.getPrice().setTotalPrice(grouponPackage.getPrice());
        result.getPrice().setTotalVipPrice(grouponPackage.getVipPrice());
        result.getPrice().setTotalCostPrice(grouponPackage.getCostPrice());
        int grouponPrice = grouponPackage.getOriginPrice() - grouponPackage.getPrice();
        result.getPrice().setGrouponPrice(grouponPrice);
        result.getPrice().setPayPrice(grouponPackage.getPrice());
        TradePriceCalculatorHelper.addPromotion(result, grouponPackage,
                param.getUserId(), "团购套餐优惠", PromotionTypeEnum.VIP.getValue(),
                StrUtil.format("团购套餐优惠：省 {} 元", formatPrice(grouponPrice)),
                grouponPrice);
    }

    private void calculateVipPrice(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) {
        GrouponPackageRespDTO grouponPackage = result.getGrouponPackage();
        int vipPrice = grouponPackage.getOriginPrice() - grouponPackage.getVipPrice();
        result.getPrice().setVipPrice(vipPrice);
        result.getPrice().setPayPrice(grouponPackage.getVipPrice());
        TradePriceCalculatorHelper.addPromotion(result, grouponPackage,
                param.getUserId(), "团购套餐会员价", PromotionTypeEnum.VIP.getValue(),
                StrUtil.format("团购套餐会员价：省 {} 元", formatPrice(vipPrice)),
                vipPrice);
    }

}
